﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;

namespace BLL
{
    public class GuestOrder
    {
        DAL.GuestOrderTableAdapters.GuestOrderTableAdapter DGO =
            new DAL.GuestOrderTableAdapters.GuestOrderTableAdapter();

        MODEL.GuestOrder _MGO = new MODEL.GuestOrder();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MGO"></param>
        /// <returns></returns>
        public MODEL.GuestOrder GetDataByID(MODEL.GuestOrder MGO)
        {
            try
            {
                DataTable DT = DGO.GetDataByID(MGO.GO_ID);

                _MGO.GO_Content = DT.Rows[0]["GO_Content"].ToString();
                _MGO.GO_DateTime = Convert.ToDateTime(DT.Rows[0]["GO_DateTime"]);
                _MGO.GO_Email = DT.Rows[0]["GO_Email"].ToString();
                _MGO.GO_ID = Convert.ToInt32(DT.Rows[0]["GO_ID"]);
                _MGO.GO_IP = DT.Rows[0]["GO_IP"].ToString();
                _MGO.GO_ItemNumber = DT.Rows[0]["GO_ItemNumber"].ToString();
                _MGO.GO_Name = DT.Rows[0]["GO_Name"].ToString();
                _MGO.GO_Tel = DT.Rows[0]["GO_Tel"].ToString();
                _MGO.GO_Title = DT.Rows[0]["GO_Title"].ToString();

                return _MGO;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加订单
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="MGO"></param>
        public void Add(MODEL.GuestOrder MGO)
        {
            try
            {
                DataTable DT_IPOrderByDateTime = DGO.GetDataByIPOrderByDateTime(MGO.GO_IP);
                if (DT_IPOrderByDateTime.Rows.Count != 0)
                {
                    DateTime DT_LastOrder = Convert.ToDateTime(DT_IPOrderByDateTime.Rows[0]["GO_DateTime"]);

                    TimeSpan TS = MGO.GO_DateTime - DT_LastOrder;

                    if (TS.Minutes < 2)
                    {
                        throw new Exception("同一IP两分钟内不可发送两次订单请求");
                    }
                }


                DGO.Add(MGO.GO_Title, MGO.GO_Name, MGO.GO_Email, MGO.GO_Tel, MGO.GO_ItemNumber,
                    MGO.GO_Content, MGO.GO_IP, MGO.GO_DateTime);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        #endregion


        #region 删除订单
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="MGO"></param>
        public void Del(MODEL.GuestOrder MGO)
        {
            try
            {
                DGO.Del(MGO.GO_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
